package com.course.designpatterns.singleton;

/**
 * @author ：Jack Hao
 * @date ：Created in 2021/6/20 20:53
 * @description：懒汉式 同步锁 两次判断
 *
 */
public class Mgr06
{
    //需要加上 volatile 不加会有汇编指令重排的问题
    private static volatile Mgr06 INSTANCE;

    private Mgr06()
    {
    }

    ;

    public static Mgr06 getInstance()
    {

        //两次判断

        if (INSTANCE == null)
        {
            synchronized (Mgr06.class)
            {
                if(INSTANCE == null){
                    try
                    {
                        Thread.sleep(20);
                    } catch (InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                    INSTANCE = new Mgr06();
                }
            }
        }

        return INSTANCE;
    }

    public static void main(String[] args)
    {


        for (int i = 0; i < 100; i++)
        {
            new Thread(() ->
            {
                System.out.println(getInstance().hashCode());
            }).start();
        }

    }


}
